/**
 * @file SequencePlayer.as
d:/flex_sdk_4.5/bin/amxmlc SequencePlayer.as

ffmpeg -i "_scene.avi" -r 12 -q:v 2 -f image2 %d.jpeg
 *  
 * @author db0@qq.com
 * @version 1.0.1
 * @date 2015-05-08
 */
package
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import flash.utils.Timer;

	/**
	 * 序列帧播放器
	 * @author
	 */
	//[SWF(width="350",height="500",frameRate="12",backgroundColor=0x000000)] 
	[SWF(width="240",height="320",frameRate="12",backgroundColor=0x000000)] 
		public class SequencePlayer extends Sprite
		{
			private var _index:int = 0;

			private var _isCyclicRotation:Boolean;
			private var _rotationDirec:int;
			private var _autoPlay:Boolean = false;
			private var _stoped:Boolean = true;

			private const _fps:uint = 25;

			private var pic_path_arr:Array = [];

			/**
			 *
			 * @param	path  目录名
			 * @param	isCyclicRotation 是否循环转动
			 * @param	rotationDirec  旋转方向 1=正 0=反
			 */
			public function SequencePlayer(path:Array = null, autoPlay:Boolean = true, isCyclicRotation:Boolean = true, rotationDirec:int = 1)
			{
				if(stage)
				{
					ww = stage.stageWidth;
					hh = stage.stageHeight;
				}
				if (path as Array)
				{
					pic_path_arr = path as Array;
				}
				else
				{

					var i:int = 0;
					while(i<30)
					{
						++i;
						var _path:String = i + ".png";
						while(_path.length <8)_path = "0"+_path;
						pic_path_arr.push("guanyin/"+_path);
					}
					//pic_path_arr = SwfLoader.filesInDir(path,SwfLoader.imgReg);
				}
				_isCyclicRotation = isCyclicRotation;
				_rotationDirec = rotationDirec;
				_autoPlay = autoPlay;
				_stoped = !_autoPlay;
				addEventListener(Event.REMOVED_FROM_STAGE, removeStage);
				start_play();
			}

			public function start_play():void
			{
				_stoped = false;
				clearTimeout(timeoutId);
				clearTimeout(nextFrameTimeoutId);
				if (pic_path_arr)
					play(0);
			}

			public function stop_play():void
			{
				_stoped = true;
				clearTimeout(timeoutId);
				clearTimeout(nextFrameTimeoutId);
			}

			private var ww:int = 640;
			private var hh:int = 480;

			public function setSize(w:int, h:int):void
			{
				ww = w;
				hh = h;
			}

			private function removeStage(e:Event):void
			{
				stop_play();
				//removeEventListener(Event.REMOVED_FROM_STAGE, removeStage);
				//upHandler(null);
				removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			}

			public function get index():int
			{
				return _index;
			}

			private function init():void
			{
				play(0);
			}



			//**************** 交互操作 ******************
			private var _downPoint:Point;
			private const _step:int = 8;
			private var tmpIndex:int = 0;
			private var mouseIsDown:Boolean = false;

			private function downHandler(e:MouseEvent):void
			{
				//autoDelay();
				mouseIsDown = true;
				_downPoint = new Point(mouseX, mouseY);
				addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
				addEventListener(MouseEvent.MOUSE_UP, upHandler);
			}

			private function upHandler(e:MouseEvent):void
			{
				mouseIsDown = false;
				removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
				removeEventListener(MouseEvent.MOUSE_UP, upHandler);
				play_next();
			}

			private function moveHandler(e:MouseEvent):void
			{
				//autoDelay();
				var i:int = tmpIndex;
				tmpIndex = Math.round((mouseX - _downPoint.x) / _step);
				if (_rotationDirec == 1)
				{
					if (tmpIndex != i)
						play((tmpIndex > i) ? -1 : 1);
				}
				else
				{
					if (tmpIndex != i)
						play((tmpIndex > i) ? 1 : -1);
				}
			}

			public function play(i:int):void
			{
				clearTimeout(timeoutId);
				clearTimeout(nextFrameTimeoutId);
				addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
				_index += i;
				var _images:Array = pic_path_arr;

				if (_isCyclicRotation)
				{
					while (_index < 0)
						_index += _images.length;
					while (_index > _images.length - 1)
						_index -= _images.length;
				}
				else
				{
					if (_index < 0)
						_index = 0;
					if (_index > _images.length - 1)
						_index = _images.length - 1;
				}
				//removeChildren();
				dispatchEvent(new Event(Event.CHANGE));
				var url:String = pic_path_arr[_index];
				//trace("3:",i,index,url);
				//trace(url);
				if (url)
				{
					SwfLoader.SwfLoad(String(pic_path_arr[_index]), addbmp);
				}
				else
				{
					trace("no pic url");
				}
			}
			private var lastdate:Date = new Date;
			private var timeoutId:uint;
			private var nextFrameTimeoutId:uint;

			private function addbmp(e:Event):void
			{
				if (e && e.type == Event.COMPLETE)
				{
					var bmp:Bitmap = e.target.content as Bitmap;
				}
				else
				{
					trace(e);
				}
				var wait_time:int = 1000 / _fps - (Number(new Date) - Number(lastdate));
				if (wait_time <= 0)
				{
					show_pic(bmp);
				}
				else
				{
					clearTimeout(timeoutId);
					timeoutId = setTimeout(show_pic, wait_time, bmp);
				}
			}

			private function show_pic(bmp:Bitmap = null):void
			{
				clearTimeout(timeoutId);
				clearTimeout(nextFrameTimeoutId);
				if (bmp)
				{
					ViewSet.removes(this);
					bmp.width = ww;
					bmp.height = hh;
					bmp.smoothing = true;
					addChild(bmp);
					if (playBtn)
						addChild(playBtn);
					if (pauseBtn)
						addChild(pauseBtn);
					if (backwardBtn)
						addChild(backwardBtn);
					if (forwardBtn)
						addChild(forwardBtn);
				}
				else
				{
					trace("no photo");
				}
				if(mouseIsDown){//鼠标按下暂停
					return;
				}else{//播放
					play_next();
				}

			}

			private function play_next():void
			{
				lastdate = new Date();
				if (_autoPlay && _index + 1 < pic_path_arr.length && !_stoped)
				{
					clearTimeout(nextFrameTimeoutId);
					nextFrameTimeoutId = setTimeout(play, 1000 / _fps, 1);
				}
				else if (_autoPlay && _isCyclicRotation && !_stoped)
				{
					//if(circle_backforword)pic_path_arr.reverse();//先顺序,播完后倒序播放,然后顺序再倒序,循环下去播放
					clearTimeout(nextFrameTimeoutId);
					nextFrameTimeoutId = setTimeout(play, 1000 / _fps, 1);
				}
				else// if(!_stoped)
				{
					//_stoped = true;
					dispatchEvent(new Event(Event.COMPLETE));
				}
			}







			private var playBtn:Sprite;
			private var pauseBtn:Sprite;
			private var backwardBtn:Sprite;
			private var forwardBtn:Sprite;

			public function initBtns(_playBtn:Sprite = null, _pauseBbtn:Sprite = null, _backwardBtn:Sprite = null, _forwardBtn:Sprite = null):void
			{
				if (_playBtn)
				{
					playBtn = _playBtn;
					addChild(playBtn);
					playBtn.addEventListener(MouseEvent.CLICK, plays);
				}
				if (_pauseBbtn)
				{
					pauseBtn = _pauseBbtn;
					addChild(pauseBtn);
					pauseBtn.addEventListener(MouseEvent.CLICK, pause);
				}
				if (_backwardBtn)
				{
					backwardBtn = _backwardBtn;
					addChild(backwardBtn);
					backwardBtn.addEventListener(MouseEvent.CLICK, backward);
				}
				if (_forwardBtn)
				{
					forwardBtn = _forwardBtn;
					addChild(forwardBtn);
					forwardBtn.addEventListener(MouseEvent.CLICK, forward);
				}
			}

			public function plays(e:Event = null):void
			{
				if (_stoped)
				{
					_stoped = false;
				}
				if (playBtn)
					playBtn.visible = false;
				if (pauseBtn)
					pauseBtn.visible = true;
				clearTimeout(nextFrameTimeoutId);
				if (_index + 1 < pic_path_arr.length)
					nextFrameTimeoutId = setTimeout(play, 1000 / _fps, 1);
			}

			public function pause(e:Event = null):void
			{
				if (_stoped)
				{
					return;
				}
				_stoped = true;
				if (playBtn)
					playBtn.visible = true;
				if (pauseBtn)
					pauseBtn.visible = false;
				clearTimeout(nextFrameTimeoutId);
			}

			public function backward(e:Event = null):void
			{
				if (_index + _step < pic_path_arr.length)
				{
					pause();//按钮按下暂停播放
					play(_step);
				}
			}

			public function forward(e:Event = null):void
			{
				if (_index - _step > 0)
				{
					pause();//按钮按下暂停播放
					play(-_step);
				}
			}

		}

}
